package com.oocl.userinfo.util;

import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.tomcat.util.codec.binary.Base64;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

import com.oocl.userinfo.pojo.User;

public class HTMLUtil {

	public static void writeAllUsersHTML(List<User> users, Writer out) {
		Element root = new Element("html");
		
		Element head = new Element("head");
		Element title = new Element("title");
		
		title.setText("All Users");
		head.addContent(title);
		
		Element style = new Element("style");
		style.setAttribute("type", "text/css");
		style.setText("table{border-collapse:collapse;border:solid 1px black;}table td{border:solid 1px black;padding:5px;}");
		head.addContent(style);
		
		root.addContent(head);
		
		Element body = new Element("body");
		
		Element backLink = new Element("a");
		backLink.setAttribute("href", "register.html");
		backLink.setText("<< Register");
		body.addContent(backLink);
		
		Element table = new Element("table");
		
		Element thead = new Element("thead");
		Element headTr = new Element("tr");
		
		Element unameTh = new Element("th");
		unameTh.setText("Name");
		headTr.addContent(unameTh);
		
		Element salTh = new Element("th");
		salTh.setText("Salary");
		headTr.addContent(salTh);
		
		Element birthTh = new Element("th");
		birthTh.setText("Birthday");
		headTr.addContent(birthTh);
		
		Element sexTh = new Element("th");
		sexTh.setText("Sex");
		headTr.addContent(sexTh);
		
		Element headTh = new Element("th");
		headTh.setText("Head");
		headTr.addContent(headTh);
		
		thead.addContent(headTr);
		table.addContent(thead);
		
		for (User u : users) {
			Element tr = new Element("tr");
			
			Element unameCol = new Element("td");
			unameCol.setText(u.getUname());
			tr.addContent(unameCol);
			
			Element salCol = new Element("td");
			salCol.setText(Double.toString(u.getSal()));
			tr.addContent(salCol);
			
			Element birthCol = new Element("td");
			birthCol.setText(new SimpleDateFormat("yyyy-MM-dd").format(u.getBirth()));
			tr.addContent(birthCol);
			
			Element sexCol = new Element("td");
			sexCol.setText(u.getSex() ? "Male" : "Female");
			tr.addContent(sexCol);
			
			Element headCol = new Element("td");
			if (u.getHead() != null) {
				Element headImg = new Element("img");
				headImg.setAttribute("src", imgBytesToBase64(u.getHead()));
				headCol.addContent(headImg);
			}
			tr.addContent(headCol);
			
			table.addContent(tr);
		}
		
		body.addContent(table);
		
		root.addContent(body);
		
		XMLOutputter outputter = new XMLOutputter();
		try {
			outputter.output(root, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void writeErrorMsgHTML(String msg, Writer out) {
		Element root = new Element("html");
		
		Element head = new Element("head");
		Element title = new Element("title");
		
		title.setText("All Users");
		head.addContent(title);
		
		root.addContent(head);
		
		Element body = new Element("body");
		
		Element backLink = new Element("a");
		backLink.setAttribute("href", "javascript:history.back();");
		backLink.setText("<< Back");
		body.addContent(backLink);
		
		Element h1 = new Element("h1");
		h1.setText(msg);
		body.addContent(h1);
		
		root.addContent(body);
		
		XMLOutputter outputter = new XMLOutputter();
		try {
			outputter.output(root, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static String imgBytesToBase64(byte[] bytes) {
		return "data:image/jpeg;base64," + Base64.encodeBase64String(bytes);
	}
}
